package com.springboot.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springboot.model.po.*;
import com.springboot.mapper.CouponMapper;
import com.springboot.mapper.OrdersMapper;
import com.springboot.mapper.UserCouponMapper;
import com.springboot.mapper.UserMapper;
import com.springboot.service.CreditService;
import com.springboot.service.UserCouponService;

import util.UserCouponUtil;

@Service
public class UserCouponServiceImpl implements UserCouponService{
	@Autowired
	UserCouponMapper userCouponMapper;
	@Autowired
	CouponMapper couponMapper;
	@Autowired
	OrdersMapper ordersMapper;
	@Autowired
	UserMapper userMapper;
	@Autowired
	CreditService creditService;
	
	@Override
	public List<Integer> listUserCoupon(String userId) {
		return userCouponMapper.listUserCoupon(userId);
	}
	
	@Override
	public List<UserCoupon> listUserCouponByOrderTotal(String userId, BigDecimal orderTotal) {
		//获取用户所有的券
		List<Integer> list = userCouponMapper.listUserCoupon(userId);
		List<UserCoupon> result = new ArrayList();
		int labels[] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0};
		for(Integer a : list) {//a就是每个优惠券类型
			Coupon coupon = couponMapper.getCoupon(a);
			//获取起用费并判断
			if(coupon.getUsePrice().compareTo(orderTotal) <= 0) {
				//达到要求加入这个优惠券
				labels[a-1] = 1;
				result.add(userCouponMapper.getUerCoupon(userId, a));
			}else {//没达到使用要求的返回total=0
				labels[a-1] = 1;
				UserCoupon userCoupon = UserCouponUtil.filterNull(a, userId);
				result.add(userCoupon);
			}
		}
		//对于用户不拥有的也需要设置
		for(int i = 0 ; i < 10 ; i++) {
			if(labels[i] == 0) {
				UserCoupon userCoupon = UserCouponUtil.filterNull(i + 1 , userId);
				result.add(userCoupon);
			}
		}
		return result;
	}
	
	@Override
	public BigDecimal useCoupon(Integer typeId , Integer orderId) {
		Orders orders = ordersMapper.getOrdersById(orderId);
		//获取当前订单价格
		BigDecimal orderTotal = orders.getOrderTotal();
		//获取优惠券价值
		BigDecimal del = couponMapper.getCouponValues(typeId).getValues();
		//起用价格
		BigDecimal usePrice = couponMapper.getCouponValues(typeId).getUsePrice();
		if(orderTotal.compareTo(usePrice) < 0)
			return BigDecimal.ZERO;
		//优惠后价格
		BigDecimal orderTotalNow = orderTotal.subtract(del);
		return orderTotalNow;
	}

	@Override
	public Integer updateUserCounpon(String userId , Integer typeId) {
		//获取目前优惠券的数量
		Integer quantity = userCouponMapper.getCouponQuantity(userId, typeId);
		int result = 0;
		if(quantity == null)
			return 0;
		if(quantity == 1) {
			//删除一项
			result = userCouponMapper.deleteUserCoupon(userId, typeId);
		}else {
			//-1
			result = userCouponMapper.updateUserCoupon(userId, typeId);
		}
		return result;
	}

	@Override
	public Integer getUserCouponQuantity(String userId,Integer typeId) {
		return userCouponMapper.getCouponQuantity(userId, typeId);
	}

	@Override
	public Integer saveUserCoupon(String userId, Integer typeId) {
		// TODO Auto-generated method stub
		BigDecimal points = userMapper.getPoints(userId);
		Coupon coupon = couponMapper.getCoupon(typeId);
		if(points.compareTo(coupon.getPoints()) <= 0) {
			//买不起优惠券
			return 0;
		}
		BigDecimal pointsNow = points.subtract(coupon.getPoints());
		//记录流水+更新删除位+过期处理
		Credit credit = new Credit();
		credit.setChannelType(1);//兑换消耗
		credit.setNum(coupon.getPoints().negate());//买优惠券了
		credit.setUserId(userId);
		creditService.insertCredit(credit);
		//更新用户总积分
		userMapper.updatePoints(userId, pointsNow);
		Integer quantity = userCouponMapper.getCouponQuantity(userId, typeId);
		if(quantity==null) {
			return userCouponMapper.insertUserCoupon(userId, typeId);
		}
		else {
			return userCouponMapper.addUserCoupon(userId, typeId);
		}
	}

	@Override
	public List<UserCoupon> listUserCouponAll(String userId) {
		// TODO Auto-generated method stub
		return userCouponMapper.listUserCouponAll(userId);
	}

	@Override
	public Integer hasUserCoupon(String userId, BigDecimal orderTotal) {
		//获取用户所有的券
		List<Integer> list = userCouponMapper.listUserCoupon(userId);
		int label = 0;
		for(Integer a : list) {//a就是每个优惠券的typeId
			//获取起用费
			BigDecimal usePrice = couponMapper.getCouponUsePrice(a);
			if(usePrice.compareTo(orderTotal) <= 0) {
				label = 1;
			}
		}
		return label;
	}
	
	

	
}
